# coding:utf-8
__author__ = 'Ybj'
'''
description:语音作业页
'''
from src.common import Base_page
from appium.webdriver.common import mobileby
from airtest.core.api import *
from appium.webdriver.webelement import *
from poco.drivers.android.uiautomation import AndroidUiautomationPoco
from poco.proxy import *
import time,datetime,locale
# global sendkey,tVname
from src.common.datetime import *
from testdata.workdata import *
from src.common.baseUrl import *
from src.common.configure import *


# poco= AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False,force_restart = True)
# #获取档期日期
dd=get_current_date(is_chinese=True)

url= baspath()
teaV_W_url=baspath()+"\\pages\\image\\work\\"
# auto_setup(__file__)
device=configure().get_driver()

class teaVoice_page(Base_page.Base_page):

    # 页面标题
    # by = mobileby.MobileBy()
    tVtitle =device.find_element_by_accessibility_id("发布学习/通知")
    # tVtitle = ("ACCESSIBILITY_ID", "发布学习/通知")
    # 练习名称
    tVname =device.find_element_by_accessibility_id(dd + r" 学习/通知")

    # 发送范围标签
    tVsetClass =device.find_element_by_accessibility_id("发送范围")
    # tVsetClass = ("ACCESSIBILITY_ID", "发送范围")
    # 确定键
    tVconfirm =device.find_element_by_accessibility_id("确定")
    # tVconfirm =("ACCESSIBILITY_ID", "确定")
    # 开始录音
    tVstart =device.find_element_by_accessibility_id("按下开始")
    # tVstart =("ACCESSIBILITY_ID", "按下开始")
    # 重录键
    tVrecord=device.find_element_by_accessibility_id("重录")
    # tVrecord =("ACCESSIBILITY_ID", "重录")
    # 发布键

    sendkey = device.find_element_by_accessibility_id("发布")
    # 发布方式语音
    voice = device.find_element_by_accessibility_id("语音")
    # 重录键
    recordkey = device.find_element_by_accessibility_id("重录")
    # 数据为空提示
    nodata = device.find_element_by_accessibility_id("数据为空")
    # 数据为空提示确认键
    # nodata_confirm = Template(teaV_W_url + "确定键.png", record_pos=(-0.003, 0.003), resolution=(1200, 1920))
    nodata_confirm=device.find_element_by_accessibility_id("数据为空").parent().parent().children()[1].children()[0]
    # nodate=("ACCESSIBILITY_ID", "数据为空").parent()
    # nodateup=nodate.parent().children()[1].children()[0]
    # 为设置发送范围提醒页的确认键
    # noClass_confirm=poco("请选择发送范围!").parent().children()[2]
    # 发布作业时，确认发布框
    claP_confirm = device.find_element_by_accessibility_id("将学习布置给")
    # 发布成功
    sendSess =device.find_element_by_accessibility_id("发布成功")

    noCla = device.find_element_by_accessibility_id("请选择发送范围!")

    allSel = device.find_element_by_accessibility_id("全选")
    # 权限提醒
    is_confirm=device.find_elements_by_id("android:id/alertTitle")
    # is_confirm =("ID","android:id/alertTitle")
    # 权限确认
    ok_confirm =device.find_elements_by_id("com.android.packageinstaller:id/permission_allow_button")
    # ok_confirm = ("ID","com.android.packageinstaller:id/permission_allow_button")


    # ******************************页面元素   结束*************************
    # ******************************页面操作   开始*************************
    #设置作业名称
    def tVsetName(self):
        teaVoice_page.click_element(teaVoice_page.tVname)
        for i in range(1,13):
            keyevent("del")
        text(vtestName)
    #录音操作
    def cord(self):
        #录制语音
        if teaVoice_page.is_confirm.exists():
            teaVoice_page.click_element(teaVoice_page.ok_confirm)
        teaVoice_page.long_click_element(teaVoice_page.tVstart,vtestTime)

    #重录操作
    def record(self):
        #重新录制
        if teaVoice_page.recordkey.exists():
            teaVoice_page.click_element(teaVoice_page.recordkey)
            teaVoice_page.long_click_element(teaVoice_page.tVstart, vtestTime)
        else:
            teaVoice_page.cord()
            teaVoice_page.click_element(teaVoice_page.recordkey)
            teaVoice_page.long_click_element(teaVoice_page.tVstart, vtestTime)
    #设置发送范围方法
    def setClass(self):
        """
        发送范围对象出现2个时，认为没有设置发送范围。
        根据发送范围标签，计算发送范围选择区域位置，并点击
        :return:
        """
        if len(teaVoice_page.tVsetClass)>1:
            m=teaVoice_page.tVsetClass[1]
            teaVoice_page.click_element(m)
            # n = teaVoice_page.tVsetClass.get_position()
            # l = poco.get_screen_size()
            # teaVoice_page.click_element((int(l[0] * n[0]) * 2 + 50), int(n[1] * l[1]))
            sleep(2)
            teaVoice_page.click_element(teaVoice_page.allSel)
            sleep(2)
            teaVoice_page.click_element(teaVoice_page.tVconfirm)
            sleep(2)
    # 发送语音作业
    def vsend(self):
        teaVoice_page.click_element(teaVoice_page.sendkey)
        #如果存在noda提示则点击确认，并切换为语音
        if teaVoice_page.nodata.exists():
            teaVoice_page.click_element(teaVoice_page.nodata_confirm)
            teaVoice_page.click_element(teaVoice_page.voice)
            teaVoice_page.click_element(teaVoice_page.sendkey)
            #存在班级发送确认则点击确认
        if teaVoice_page.noCla.exists():
            teaVoice_page.click_element(teaVoice_page.noClass_confirm)
            sleep(1)
            teaVoice_page.setClass(self)
            teaVoice_page.click_element(teaVoice_page.sendkey)
        for i in range(0,60):
            if not teaVoice_page.claP_confirm.exists():
                sleep(2)
            else:
                break
        teaVoice_page.click_element(teaVoice_page.tVconfirm)

     # ******************************页面操作   结束*************************